﻿using JewererBasic.BO;
using JewererBasic.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace JewererBasic.Controllers
{
    public class MCController : Controller
    {
        public ActionResult BagControl(string bagId= null)
        {
            this.SetViewBag();
            if(string.IsNullOrEmpty(bagId)==false)
            {
                NewMCViewModel model = new NewMCViewModel();
                MCBagBO bagBO = new MCBagBO();
                model.Bag = bagBO.FindBag(bagId);
                if (model.Bag == null)
                    return View();
                model.Current_Bag_Process = bagBO.GetCurrentProcess(model.Bag);
                model.Next_Bag_Process = new BAG_PROCESS();
                
                model.Next_Bag_Process.DateIn = DateTime.Now;
                model.Current_Bag_Process_History = bagBO.GetCurrentProcessHistory(model.Bag);
                model.Next_Bag_Process_History = new BAG_PROCESS_HISTORY();                
                ViewBag.ListBagProcess = new SelectList(model.Bag.BAG_PROCESS, "IdProcess", "PROCESS.Name");

                return View(model);
            }
            return View();
        }

        [HttpPost]
        public ActionResult BagControl(NewMCViewModel model, FormCollection form)
        {
            MCBagBO bagBO = new MCBagBO();
            string bagId = "";
            if (form["submit"].Equals("Search"))
            {
                bagId = form["txtBagId"];
            }
            else if (form["submit"].Equals("Done"))
            {
                bagBO.Update(model);
                bagId = model.Bag.Id;
            }
            else if (form["submit"].Equals("Start"))
            {
                bagBO.StartBag(model.Bag.Id);
                bagId = model.Bag.Id;
            }
            else if (form["submit"].Equals("Finish"))
            {
                bagBO.FinishBag(model);
                bagId = model.Bag.Id;
            }
            else if (form["submit"].Equals("Restart"))
            {
                bagBO.Restart(model.Bag.Id);
                bagId = model.Bag.Id;
            }
            return RedirectToAction("BagControl", new { bagId = bagId });
        }

        public JsonResult FindBag(string bagId)
        {
            MCBagBO bo = new MCBagBO();
            if (bo.FindBag(bagId) == null)
                return Json(new { Available=false }, JsonRequestBehavior.AllowGet);
            return Json(new { Available=true}, JsonRequestBehavior.AllowGet);
        }

        public JsonResult CheckIfRework(int processId, string bagId)
        {
            MCBagBO bo = new MCBagBO();
            bool isRework = bo.IsRework(processId, bagId);
            return Json(new { isRework=isRework}, JsonRequestBehavior.AllowGet);
        }

        public JsonResult CheckIfWaitingProcess(int processId)
        {
            MCBagBO bo = new MCBagBO();
            bool isWaiting = bo.CheckIfWaitingProcess(processId);
            return Json(new { isWaiting = isWaiting}, JsonRequestBehavior.AllowGet);
        }

        public void SetViewBag()
        {
            MSQDBEntities db = new MSQDBEntities();
            ViewBag.Workers = new SelectList(db.EMPLOYEEs.ToList(), "Id", "Staff_Id");
        }
    }
}
